package com.ysd.lis.service.eq.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.eq.*;
import com.ysd.lis.mapper.eq.*;
import com.ysd.lis.request.CommonPageDto;
import com.ysd.lis.service.SysRuleSetService;
import com.ysd.lis.service.eq.EqSpotcheckPlanEqStItService;
import com.ysd.lis.service.eq.EqSpotcheckPlanEqStService;
import com.ysd.lis.service.eq.EqSpotcheckPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 设备点检计划 服务实现类
 * </p>
 *
 * @author WH
 * @since 2024-08-26
 */
@Service
public class EqSpotcheckPlanServiceImpl extends ServiceImpl<EqSpotcheckPlanMapper, EqSpotcheckPlan> implements EqSpotcheckPlanService {

    @Autowired
    private EqSpotcheckPlanMapper eqSpotcheckPlanMapper;
    @Autowired
    private EqSpotcheckTaskMapper eqSpotcheckTaskMapper;

    @Autowired
    private EqSpotcheckTaskEqMapper eqSpotcheckTaskEqMapper;

    @Autowired
    private EqSpotcheckTaskEqStMapper eqSpotcheckTaskEqStMapper;

    @Autowired
    private EqSpotcheckTaskEqStItMapper eqSpotcheckTaskEqStItMapper;
    @Autowired
    private EqSpotcheckPlanEqMapper eqSpotcheckPlanEqMapper;

    @Autowired
    private EqSpotcheckPlanEqStMapper eqSpotcheckPlanEqStMapper;
    @Autowired
    private EqSpotcheckPlanEqStItMapper eqSpotcheckPlanEqStItMapper;

    @Autowired
    private EqSpotcheckPlanEqStService eqSpotcheckPlanEqStService;
    @Autowired
    private EqSpotcheckPlanEqStItService eqSpotcheckPlanEqStItService;

    @Autowired
    public SysRuleSetService sysRuleSetService;
    @Override
    public Result findSpotcheckPlanPageList(CommonPageDto pageParam) {
        Page<EqSpotcheckPlan> page = new Page<>();
        page.setCurrent(pageParam.getPageIndex());
        page.setSize(pageParam.getPageSize());
        LambdaQueryWrapper<EqSpotcheckPlan> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(EqSpotcheckPlan::getDelFlag, 0);
        if (StringUtils.isNotBlank(pageParam.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(EqSpotcheckPlan::getCode, pageParam.getSeachValue()).or().like(EqSpotcheckPlan::getName, pageParam.getSeachValue()));
        }
        lambdaQueryWrapper.orderByDesc(EqSpotcheckPlan::getCreateTime);
        Page<EqSpotcheckPlan> orderPage = eqSpotcheckPlanMapper.selectPage(page, lambdaQueryWrapper);
        return Result.succ(1, "查询成功", orderPage);
    }

    @Override
    public Result addSpotcheckPlan(EqSpotcheckPlan eqSpotcheckPlan) {
        //添加设备巡检任务
        String a = sysRuleSetService.createRuleCodeList("EqSpotcheckPlanCode", 1).get(0);
        eqSpotcheckPlan.setCode(a);
        eqSpotcheckPlanMapper.insert(eqSpotcheckPlan);
        List<EqSpotcheckPlanEq> patrolPlanEqList = eqSpotcheckPlan.getSpotcheckPlanEqList();
        if (CollectionUtils.isNotEmpty(patrolPlanEqList)) {
            for (EqSpotcheckPlanEq eqPatrolPlanEq : patrolPlanEqList) {
                eqPatrolPlanEq.setPlanId(eqSpotcheckPlan.getId());
                //添加设备巡检任务设备
                eqSpotcheckPlanEqMapper.insert(eqPatrolPlanEq);
                List<EqSpotcheckPlanEqSt> patrolPlanEqStList = eqPatrolPlanEq.getSpotcheckPlanEqStList();
                List<EqSpotcheckPlanEqStIt> patrolPlanEqStItList = eqPatrolPlanEq.getSpotcheckPlanEqStItList();


                //添加巡检任务设备的巡检标准
                for (EqSpotcheckPlanEqSt eqPatrolPlanEqSt : patrolPlanEqStList) {
                    eqPatrolPlanEqSt.setPlanId(eqSpotcheckPlan.getId());
                    eqPatrolPlanEqSt.setPlanEqId(eqPatrolPlanEq.getId());
                    eqSpotcheckPlanEqStMapper.insert(eqPatrolPlanEqSt);
                    List<EqSpotcheckPlanEqStIt> eqPatrolPlanEqStIts = patrolPlanEqStItList.stream().filter(item -> item.getStandardId().equals(eqPatrolPlanEqSt.getStandardId())).collect(Collectors.toList());
                    //添加巡检任务设备的巡检标准项
                    if (CollectionUtils.isNotEmpty(eqPatrolPlanEqStIts)) {
                        for (EqSpotcheckPlanEqStIt eqPatrolPlanEqStIt : eqPatrolPlanEqStIts) {
                            eqPatrolPlanEqStIt.setPlanId(eqSpotcheckPlan.getId());
                            eqPatrolPlanEqStIt.setPlanEqId(eqPatrolPlanEq.getId());
                            eqPatrolPlanEqStIt.setPlanEqStId(eqPatrolPlanEqSt.getId());
                        }
                        eqSpotcheckPlanEqStItService.saveBatch(eqPatrolPlanEqStIts);
                    }
                }
            }
        }
        return Result.succ(1, "添加成功", eqSpotcheckPlan);
    }

    @Override
    public Result editSpotcheckPlan(EqSpotcheckPlan eqSpotcheckPlan) {
        eqSpotcheckPlanMapper.updateById(eqSpotcheckPlan);
        List<EqSpotcheckPlanEq> patrolPlanEqList = eqSpotcheckPlan.getSpotcheckPlanEqList();
        if (CollectionUtils.isNotEmpty(patrolPlanEqList)) {
            for (EqSpotcheckPlanEq eqPatrolPlanEq : patrolPlanEqList) {
                eqSpotcheckPlanEqMapper.updateById(eqPatrolPlanEq);
                List<EqSpotcheckPlanEqSt> patrolPlanEqStList = eqPatrolPlanEq.getSpotcheckPlanEqStList();
                List<EqSpotcheckPlanEqStIt> patrolPlanEqStItList = eqPatrolPlanEq.getSpotcheckPlanEqStItList();
                if (CollectionUtils.isNotEmpty(patrolPlanEqStList)) {
                    eqSpotcheckPlanEqStService.saveOrUpdateBatch(patrolPlanEqStList);
                }
                if (CollectionUtils.isNotEmpty(patrolPlanEqStItList)) {
                    eqSpotcheckPlanEqStItService.saveOrUpdateBatch(patrolPlanEqStItList);
                }
            }
        }
        return Result.succ(1, "编辑成功", eqSpotcheckPlan);
    }

    @Override
    public Result spotcheckPlanStopStart(EqSpotcheckPlan eqSpotcheckPlan) {
        eqSpotcheckPlanMapper.updateById(eqSpotcheckPlan);
        return Result.succ(1, "操作成功", null);
    }

    @Override
    public Result handleExecutePlan(EqSpotcheckPlan eqSpotcheckPlan) {
        String a = sysRuleSetService.createRuleCodeList("EqSpotcheckTaskCode", 1).get(0);
        //新增任务
        EqSpotcheckPlan patrolPlan = eqSpotcheckPlanMapper.selectById(eqSpotcheckPlan.getId());
        EqSpotcheckTask eqPatrolTask = new EqSpotcheckTask();
        BeanUtil.copyProperties(patrolPlan, eqPatrolTask);
        eqPatrolTask.setId(null);
        eqPatrolTask.setPlanId(patrolPlan.getId());
        eqPatrolTask.setCode(a);
        eqPatrolTask.setStatus("0");
        eqSpotcheckTaskMapper.insert(eqPatrolTask);
        List<EqSpotcheckPlanEq> eqPatrolPlanEqs = eqSpotcheckPlanEqMapper.selectList(new LambdaQueryWrapper<EqSpotcheckPlanEq>().eq(EqSpotcheckPlanEq::getPlanId, eqSpotcheckPlan.getId()).eq(EqSpotcheckPlanEq::getDelFlag, 0));
        for (EqSpotcheckPlanEq eqPatrolPlanEq : eqPatrolPlanEqs) {
            EqSpotcheckTaskEq eqPatrolTaskEq = new EqSpotcheckTaskEq();
            BeanUtil.copyProperties(eqPatrolPlanEq, eqPatrolTaskEq);
            eqPatrolTaskEq.setId(null);
            eqPatrolTaskEq.setTaskId(eqPatrolTask.getId());
            eqPatrolTaskEq.setPlanId(eqSpotcheckPlan.getId());
            eqPatrolTaskEq.setPlanEqId(eqPatrolPlanEq.getId());
            eqSpotcheckTaskEqMapper.insert(eqPatrolTaskEq);
            List<EqSpotcheckPlanEqSt> eqPatrolPlanEqSts = eqSpotcheckPlanEqStMapper.selectList(new LambdaQueryWrapper<EqSpotcheckPlanEqSt>().eq(EqSpotcheckPlanEqSt::getPlanId, eqSpotcheckPlan.getId()).eq(EqSpotcheckPlanEqSt::getDelFlag, 0));
            List<EqSpotcheckPlanEqStIt> eqPatrolPlanEqStIts = eqSpotcheckPlanEqStItService.list(new LambdaQueryWrapper<EqSpotcheckPlanEqStIt>().in(EqSpotcheckPlanEqStIt::getPlanId, eqSpotcheckPlan.getId()).eq(EqSpotcheckPlanEqStIt::getDelFlag, 0));

            for (EqSpotcheckPlanEqSt eqPatrolPlanEqSt : eqPatrolPlanEqSts) {
                EqSpotcheckTaskEqSt eqPatrolTaskEqSt = new EqSpotcheckTaskEqSt();
                BeanUtil.copyProperties(eqPatrolPlanEqSt, eqPatrolTaskEqSt);
                eqPatrolTaskEqSt.setId(null);
                eqPatrolTaskEqSt.setTaskId(eqPatrolTask.getId());
                eqPatrolTaskEqSt.setTaskEqId(eqPatrolTaskEq.getId());
                eqPatrolTaskEqSt.setPlanId(eqSpotcheckPlan.getId());
                eqPatrolTaskEqSt.setPlanEqId(eqPatrolPlanEq.getId());
                eqPatrolTaskEqSt.setPlanEqStId(eqPatrolPlanEqSt.getId());
                eqSpotcheckTaskEqStMapper.insert(eqPatrolTaskEqSt);
                List<EqSpotcheckPlanEqStIt> eqPatrolPlanEqStItList = eqPatrolPlanEqStIts.stream().filter(item -> item.getPlanEqStId().equals(eqPatrolPlanEqSt.getId())).collect(Collectors.toList());
                for (EqSpotcheckPlanEqStIt eqPatrolPlanEqStIt : eqPatrolPlanEqStItList) {
                    EqSpotcheckTaskEqStIt eqPatrolTaskEqStIt = new EqSpotcheckTaskEqStIt();
                    BeanUtil.copyProperties(eqPatrolPlanEqStIt, eqPatrolTaskEqStIt);
                    eqPatrolTaskEqStIt.setId(null);
                    eqPatrolTaskEqStIt.setTaskId(eqPatrolTask.getId());
                    eqPatrolTaskEqStIt.setTaskEqId(eqPatrolTaskEq.getId());
                    eqPatrolTaskEqStIt.setTaskEqStId(eqPatrolTaskEqSt.getId());
                    eqPatrolTaskEqStIt.setPlanId(eqSpotcheckPlan.getId());
                    eqPatrolTaskEqStIt.setPlanEqId(eqPatrolPlanEq.getId());
                    eqPatrolTaskEqStIt.setPlanEqStId(eqPatrolPlanEqSt.getId());
                    eqPatrolTaskEqStIt.setPlanEqStItId(eqPatrolPlanEqStIt.getId());
                    eqSpotcheckTaskEqStItMapper.insert(eqPatrolTaskEqStIt);
                }
            }
        }
        return Result.succ(1, "执行成功", null);
    }

    @Override
    public Result findSpotcheckPlanById(EqSpotcheckPlan eqSpotcheckPlan) {
        EqSpotcheckPlan eqPatrolPlan1 = eqSpotcheckPlanMapper.selectById(eqSpotcheckPlan.getId());
        List<EqSpotcheckPlanEq> eqPatrolPlanEqs = eqSpotcheckPlanEqMapper.selectList(new LambdaQueryWrapper<EqSpotcheckPlanEq>().eq(EqSpotcheckPlanEq::getPlanId, eqPatrolPlan1.getId()).eq(EqSpotcheckPlanEq::getDelFlag, 0));
        for (EqSpotcheckPlanEq eqPatrolPlanEq : eqPatrolPlanEqs) {
            List<EqSpotcheckPlanEqSt> eqPatrolPlanEqSts = eqSpotcheckPlanEqStMapper.selectList(new LambdaQueryWrapper<EqSpotcheckPlanEqSt>().eq(EqSpotcheckPlanEqSt::getPlanEqId, eqPatrolPlanEq.getId()).eq(EqSpotcheckPlanEqSt::getDelFlag, 0));
            List<EqSpotcheckPlanEqStIt> eqPatrolPlanEqStIts = eqSpotcheckPlanEqStItService.list(new LambdaQueryWrapper<EqSpotcheckPlanEqStIt>().eq(EqSpotcheckPlanEqStIt::getPlanEqId, eqPatrolPlanEq.getId()).eq(EqSpotcheckPlanEqStIt::getDelFlag, 0));
            eqPatrolPlanEq.setSpotcheckPlanEqStList(eqPatrolPlanEqSts);
            eqPatrolPlanEq.setSpotcheckPlanEqStItList(eqPatrolPlanEqStIts);
        }
        eqPatrolPlan1.setSpotcheckPlanEqList(eqPatrolPlanEqs);
        return Result.succ(1, "查询成功", eqPatrolPlan1);
    }

    @Override
    public Result deleteSpotcheckPlan(List<EqSpotcheckPlan> eqSpotcheckPlans) {
        List<String> ids = eqSpotcheckPlans.stream().map(EqSpotcheckPlan::getId).collect(Collectors.toList());
        eqSpotcheckPlanMapper.deleteBatchIds(ids);
        eqSpotcheckPlanEqMapper.delete(new LambdaQueryWrapper<EqSpotcheckPlanEq>().in(EqSpotcheckPlanEq::getPlanId, ids).eq(EqSpotcheckPlanEq::getDelFlag, 0));
        eqSpotcheckPlanEqStMapper.delete(new LambdaQueryWrapper<EqSpotcheckPlanEqSt>().in(EqSpotcheckPlanEqSt::getPlanId, ids).eq(EqSpotcheckPlanEqSt::getDelFlag, 0));
        eqSpotcheckPlanEqStItService.remove(new LambdaQueryWrapper<EqSpotcheckPlanEqStIt>().in(EqSpotcheckPlanEqStIt::getPlanId, ids).eq(EqSpotcheckPlanEqStIt::getDelFlag, 0));
        return Result.succ(1, "删除巡检任务成功", "");
    }
}
